package org.noote.roby.core;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;

public class WorldView extends View {

	private final Lock worldEdgeLock = new ReentrantLock();
	LineDetection _detect = new LineDetection();
	
	Paint _paint = new Paint();
	Bitmap _bitmap;
	Bitmap _bitmapToSee;
	int _height = 0;
	int _width = 0;
	
	int _iToWatch = 11;
	
	public WorldView(Context context) {
		super(context);
		_detect = new LineDetection();
	}
	public WorldView(Context context, AttributeSet attribs ) {
		super(context, attribs);
		_detect = new LineDetection();
	}

	@Override
	protected void onSizeChanged(int width, int height, int width_old, int height_old)
	{
		super.onSizeChanged(width, height, width_old, height_old);
		
		_width = width;
		_height = height;
		
	    if(_detect.getWidth() != _width || _detect.getHeight() != _height)
	    {
			_bitmapToSee = Bitmap.createBitmap(_width, _height, Bitmap.Config.ARGB_8888);
			_detect.createBuffer(_width, _height);
	    }
	}

	private void drawPixels()
	{
		// Set modified buffer !
		if (worldEdgeLock.tryLock()) {
			try {
				if(_detect.getBuffer() != null)
				{
					_bitmap.setPixels(_detect._output.getBuffer(), 0, _detect._output.getWidth(), 0, 0, _bitmap.getWidth(), _bitmap.getHeight());
					_bitmapToSee = Bitmap.createScaledBitmap(_bitmap, _width, _height, true);
				}
			} finally {
				worldEdgeLock.unlock();
			}
		}
		
		invalidate();
	}
	
	@Override
	protected void onDraw(Canvas canvas) {
		canvas.drawBitmap(_bitmapToSee, 0, 0, _paint);
		super.onDraw(canvas);
	}

	public void copy(int[] pixels, int width, int height) {
		if (worldEdgeLock.tryLock()) {
			try {
				if(_detect.getWidth() != width || _detect.getHeight() != height)
				{
					_detect.createBuffer(width, height);
					_bitmap = Bitmap.createBitmap(_detect.getWidth(), _detect.getHeight(), Bitmap.Config.ARGB_8888);
				}
				
				_detect.copy_from(pixels, width, height);				
				_detect.detect();
				show();
			} finally {
				worldEdgeLock.unlock();
				postInvalidate();
			}
		}
	}
	
	public void showNext() {
		_iToWatch = (_iToWatch+1)%12;
		show();
	}
	
	public void show()
	{
		// show some result
		switch(_iToWatch)
		{
		default:
		case 0:
			_detect.print_input();
			break;
		case 1:
			_detect.print_intensity();
			break;
		case 2:
			_detect.print_sobel();
			break;
		case 3:
			_detect.print_black();
			_detect.show_edges();
			break;
		case 4:
			_detect.print_input();			
			_detect.show_edges();
			break;
		case 5:
			_detect.print_black();			
			_detect.show_orientation();
			break;
		case 6:
			_detect.print_input();			
			_detect.show_orientation();
			break;
		case 7:
			_detect.print_black();
			_detect.show_ransac_grids();
			_detect.show_ransac_edgels();			
			break;
		case 8:
			_detect.print_black();
			_detect.show_ransac_grids();
			_detect.show_ransac_edgels();
			_detect.show_ransac_segments();
			break;
		case 9:
			_detect.print_intensity();
			_detect.show_ransac_grids();
			_detect.show_ransac_segments();
			break;
		case 10:
			_detect.print_black();
			_detect.show_segments();
			break;
		case 11:
			_detect.print_input();
			_detect.show_segments();
			break;
		}

		// update le resultat :)
		drawPixels();
	}
}
